﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Data;
using System.Windows.Forms;

namespace ecomerci
{
    class pdf
    {
        sqlite sql;
        applicationLogic logic;

        public pdf()
        {
            logic = new applicationLogic();
            sql = new sqlite();
        }

        public string createInvoice(int Offer_id)
        {                  
            transaction transaction = logic.getTransaction(Offer_id);
            customer customer = logic.getCustomer(int.Parse(transaction.customer_id.ToString()));
            double shippingPrice = logic.getShippingPrice(transaction.shipping.ToString());
            string file = logic.getInvoicePath() + @"\" + DateTime.Parse(transaction.date).ToString("dd-MM-yyyy") + "_" + transaction.offer_number.ToString() + ".pdf";

            BaseFont font = BaseFont.CreateFont(Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Windows), "Fonts\\arial.ttf"), BaseFont.CP1252, false);
            
            float[] relativeWidth = new float[] { 0.2f, 0.2f, 0.5f, 0.1f };
            PdfPTable table = new PdfPTable(relativeWidth);
            table.DefaultCell.Border = Rectangle.NO_BORDER;
            table.WidthPercentage = 100;
            table.AddCell("Datum");
            table.AddCell("Angebotsnummer");
            table.AddCell("Angebotstitel");
            table.AddCell("Preis");
            table.AddCell("");
            table.AddCell("");
            table.AddCell("");
            table.AddCell("");
            // date
            table.AddCell(new Phrase(transaction.date.ToString().Remove(10), new Font(font, 10.0f)));
            // offer_number
            table.AddCell(new Phrase(transaction.offer_number.ToString(), new Font(font, 10.0f)));
            // offer title
            table.AddCell(new Phrase(transaction.offer_title.ToString(), new Font(font, 10.0f, Font.ITALIC)));
            // price
            table.AddCell(new Phrase("€ " + transaction.price.ToString(), new Font(font, 10.0f)));
            table.AddCell("");
            table.AddCell("");
            table.AddCell(new Phrase("Versandkosten", new Font(font, 10.0f)));
            table.AddCell(new Phrase("€ " + shippingPrice.ToString(), new Font(font, 10.0f)));
            table.AddCell("");
            table.AddCell("");
            table.AddCell("");
            table.AddCell(new Phrase("________", new Font(font, 10.0f)));
            table.AddCell("");
            table.AddCell("");
            table.AddCell("");
            table.AddCell(new Phrase("€ " + (double.Parse(transaction.price.ToString()) + shippingPrice), new Font(font, 10.0f)));

            Phrase head = new Phrase("RECHNUNG", new Font(font, 20.0f, Font.BOLD));
            Phrase from = new Phrase("Anja Prehn • Inneboltstraße 11 • D-47506 Neukirchen-Vluyn", new Font(font, 6.0f));
            Phrase toName = new Phrase("Herr / Frau " + customer.first_name + " " + customer.last_name);
            Phrase toAdress = new Phrase(customer.adress);
            Phrase toArea = new Phrase(customer.area_code + " " + customer.city);
            Phrase logo = new Phrase("kaffeeboehnchen", new Font(font, 15f, Font.BOLDITALIC));
            Phrase sndName = new Phrase("Anja Prehn");
            Phrase sndAdress = new Phrase("Inneboltstraße 11");
            Phrase sndArea = new Phrase("D-47506 Neukirchen-Vluyn");
            
            Paragraph sender = new Paragraph();
            sender.Add(logo);
            sender.Add(Chunk.NEWLINE);
            sender.Add(sndName);
            sender.Add(Chunk.NEWLINE);
            sender.Add(sndAdress);
            sender.Add(Chunk.NEWLINE);
            sender.Add(sndArea);
            sender.Add(Chunk.NEWLINE);

            Paragraph receiver = new Paragraph();
            receiver.Add(toName);
            receiver.Add(Chunk.NEWLINE);
            receiver.Add(toAdress);
            receiver.Add(Chunk.NEWLINE);
            receiver.Add(toArea);
            
            Document invoice = new Document();

            PdfWriter writer = PdfWriter.GetInstance(invoice, new FileStream(file, FileMode.Create));
            invoice.Open();
            PdfContentByte contentByte = writer.DirectContent;
            
            ColumnText columnText = new ColumnText(contentByte);
            columnText.SetSimpleColumn(550, 826, 405, 100);

            columnText.AddText(sender);
            columnText.Go();
            
            invoice.Add(Chunk.NEWLINE);
            invoice.Add(head);
            invoice.Add(Chunk.NEWLINE);
            invoice.Add(Chunk.NEWLINE);
            invoice.Add(Chunk.NEWLINE);
            invoice.Add(from);
            invoice.Add(Chunk.NEWLINE);
            invoice.Add(receiver);
            invoice.Add(Chunk.NEWLINE);
            invoice.Add(Chunk.NEWLINE);
            invoice.Add(Chunk.NEWLINE);
            invoice.Add(Chunk.NEWLINE);
            invoice.Add(Chunk.NEWLINE);
            invoice.Add(table);
            invoice.Close();

            return file;
        }

        public string createSalesOverview(string[] DateRange)
        {
            DataTable transactions = logic.getTransactionByMonth(DateRange);
            string file = logic.getSalesOverviewPath() + @"\" + DateRange[0].ToString() + "_" + DateRange[1].ToString() + ".pdf";

            float[] relativeWidth = new float[] { 0.2f, 0.5f, 0.15f, 0.15f };
            double priceAccumulated = 0.0;
            double purchase_priceAccumulated = 0.0;

            BaseFont font = BaseFont.CreateFont(Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Windows), "Fonts\\arial.ttf"), BaseFont.CP1252, false);
            PdfPTable table = new PdfPTable(relativeWidth);
            Document salesOverview = new Document();
            PdfWriter writer = PdfWriter.GetInstance(salesOverview, new FileStream(file, FileMode.Create));

            table.DefaultCell.Border = Rectangle.NO_BORDER;
            table.WidthPercentage = 100;

            table.AddCell("Datum");
            table.AddCell("Angebotstitel");
            table.AddCell("Einkauf");
            table.AddCell("Verkauf");
            table.AddCell("");
            table.AddCell("");
            table.AddCell("");
            table.AddCell("");

            foreach (DataRow row in transactions.Rows)
            {
                table.AddCell(new Phrase(row[0].ToString().Remove(10), new Font(font, 10.0f)));
                table.AddCell(new Phrase(row[2].ToString(), new Font(font, 10.0f, Font.ITALIC)));
                table.AddCell(new Phrase("€ " + row[4].ToString(), new Font(font, 10.0f)));
                table.AddCell(new Phrase("€ " + row[3].ToString(), new Font(font, 10.0f)));

                purchase_priceAccumulated += double.Parse(row[4].ToString());
                priceAccumulated += double.Parse(row[3].ToString());
            }

            table.AddCell("");
            table.AddCell("");
            table.AddCell(new Phrase("________", new Font(font, 10.0f)));
            table.AddCell(new Phrase("________", new Font(font, 10.0f)));
            table.AddCell("");
            table.AddCell("");
            table.AddCell(new Phrase("€ " + purchase_priceAccumulated.ToString(), new Font(font, 10.0f, Font.BOLD)));
            table.AddCell(new Phrase("€ " + priceAccumulated.ToString(), new Font(font, 10.0f, Font.BOLD)));
            
            salesOverview.Open();
            salesOverview.Add(new Paragraph("UMSATZÜBERSICHT", new Font(font, 20.0f, Font.BOLD)));
            salesOverview.Add(Chunk.NEWLINE);
            salesOverview.Add(table);
            salesOverview.Close();

            return file;
        }
    }
}
